package org.jypj.dev.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.jypj.dev.dao.ICountDao;
import org.jypj.dev.dao.ILoginInfoDao;
import org.jypj.dev.dao.IUserTrainScoreDao;
import org.jypj.dev.model.LoginInfo;
import org.jypj.dev.model.Train;
import org.jypj.dev.model.UserTrainScore;
import org.jypj.dev.model.UserWork;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.StringUtil;
import org.jypj.dev.util.UserWorkUtils;
import org.jypj.dev.vo.SearchVo;
import org.jypj.dev.vo.UserCountTreeVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("countController")
public class CountController {

    @Resource
    private ICountDao countDao;

    @Resource
    private ILoginInfoDao loginInfoDao;

    @Resource
    private IUserTrainScoreDao userTrainScoreDao;

    /**
     * 跳转到日统计视图
     *
     * @return
     */
    @RequestMapping("toDayCount.action")
    public String toDayCount(HttpServletRequest request) {
        String todayString = DateUtils.getTodayString("yyyy-MM-dd");
        request.setAttribute("today", todayString);
        return "back/count/count_day_detail";
    }

    /**
     * 跳转到日统计视图
     *
     * @return
     */
    @RequestMapping("toUserCountInfo.action")
    public String toUserCountInfo(HttpServletRequest request, SearchVo searchVo) {
        LoginInfo loginInfo = loginInfoDao.selectLoginInfoById(searchVo.getLoginId());
        request.setAttribute("loginInfo", loginInfo);
        request.setAttribute("startDate", loginInfo.getPlanDate());
        request.setAttribute("trainCode", searchVo.getTrainCode());
        return "back/count/count_day_detail";
    }

    /**
     * 查询用户分数
     *
     * @param searchVo
     * @return
     */
    @RequestMapping(value = "selectUserTrainScore.action")
    @ResponseBody
    public Map<String, Object> selectUserTrainScore(SearchVo searchVo) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            UserTrainScore userTrainScore = new UserTrainScore();
            userTrainScore.setUserNum(searchVo.getUserNum());
            userTrainScore.setTrainNum(searchVo.getTrainNum());
            userTrainScore.setPlanTime(searchVo.getStartDate());
            userTrainScore.setTrainCode(searchVo.getTrainCode());
            UserTrainScore uts = userTrainScoreDao.selectObjectByUserTrainScore(userTrainScore);
            if (uts == null) {
                uts = new UserTrainScore();
                uts.setUserNum(searchVo.getUserNum());
                uts.setTrainCode(searchVo.getTrainCode());
                uts.setPlanTime(searchVo.getStartDate());
                uts.setSysScore(-1);
                uts.setModScore(-1);
            } else {
                String modUser = uts.getModUser();
                if (StringUtil.isEmpty(modUser)) {
                    uts.setModScore(-1);
                }
            }
            map.put("data", uts);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 查询某个人在某个车次上的工序
     *
     * @param loginId
     * @param trainCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectUserWorkByLoginId.action")
    public Map<String, Object> selectUserWorkByLoginId(String startDate, String loginId, String trainCode) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        String userWorkTableName = StringUtil.getUserWorkTableNameFromDate(startDate);
        int code = 0;
        try {
            queryMap.put("loginId", loginId);
            queryMap.put("tableName", userWorkTableName);
            List<Train> trains = countDao.selectAllTrain(queryMap);
            if (trains.size() > 0) {
                trainCode = StringUtil.isEmpty(trainCode) ? trains.get(0).getCode() : trainCode;
                queryMap.put("trainCode", trainCode);
                List<UserWork> userWorks = countDao.selectUserWorkByLoginId(queryMap);
                UserWorkUtils.initUserWorkCatelogBeginFinish(userWorks);//初始化目录的开始和结束时间
                UserWorkUtils.initUserWorkStatus(userWorks);
                UserWorkUtils.initUserWorkScore(userWorks);// 初始化一级工序的分数
                map.put("datas", userWorks);
            } else {
                map.put("datas", new ArrayList<UserWork>());
            }
            map.put("trains", trains);
            map.put("trainCode", trainCode);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 查询某个人在某个车次上的工序
     *
     * @param loginId
     * @param trainCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectUserWorkByTrainCode.action")
    public Map<String, Object> selectUserWorkByTrainCode(String startDate, String loginId, String trainCode) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        String userWorkTableName = StringUtil.getUserWorkTableNameFromDate(startDate);
        int code = 0;
        try {
            queryMap.put("loginId", loginId);
            queryMap.put("tableName", userWorkTableName);
            queryMap.put("trainCode", trainCode);
            List<UserWork> userWorks = countDao.selectUserWorkByLoginId(queryMap);
            UserWorkUtils.initUserWorkCatelogBeginFinish(userWorks);//初始化目录的开始和结束时间
            UserWorkUtils.initUserWorkStatus(userWorks);
            UserWorkUtils.initUserWorkScore(userWorks);
            map.put("datas", userWorks);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }

}
